<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

/**
 * Помогает при печати главной страницы каталога
 *
 * @author chaporgin_a
 */
class VA_Helper_HiLed_Catalog {
    const M_HILED = 'HI LED LIGHT';
    const M_DLIGHT = 'DIAMOND LIGHT';
    const M_LAMP = 'Лампа и лоток';
    const M_EXH = 'Оборудование для выставок';
    protected $elements;
    public function __construct($categories) {
        $this->elements = array();
        foreach($categories as $category) {
            if(in_array($category->category_name, array(self::M_HILED, self::M_DLIGHT, self::M_EXH, self::M_LAMP))) {
                $route = JRoute::_(sprintf('&option=com_hiled&view=category&id=%d&Itemid=%d'
                        , $category->category_id
                        , JRequest::getInt("Itemid")
                ));
                
                $this->elements[$category->category_name] = array(
                        'route' => $route
                        , 'category' => $category
                        ,   'image' => $this->provideImage($category->category_name)

                );
            }
        }
    }
    protected function provideImage($name) {
        $pref = '/templates/hiled/images/catalog/';
        switch ($name) {
            case self::M_DLIGHT:
                return $pref.'dl.jpg';
            case self::M_EXH:
                return $pref.'exh.jpg';
            case self::M_HILED:
                return $pref.'hiled.jpg';
            case self::M_LAMP:
                return $pref.'lamp.jpg';
            default:
                throw new Exception('unknown name');
        }
    }
    public function getMouseoverScript($element) {
        $r = $this->getInfo($element);
        if(empty ($r)) return null;
        $pos = strrpos($r['image'], '.');
        if( FALSE === $pos) return null;
        $image = $r['image'];
//        return 'alert(this.style.backgroundImage);';
        return sprintf("jQuery(this).css({'background-image':'url(%s)'});"
                , substr($image, 0, $pos).'hov'.substr($image, $pos)
        );
    }
    public function getMouseoutScript($element) {
        $r = $this->getInfo($element);
        if(empty ($r)) return null;
//        return 'alert(this.style.backgroundImage);';
        return sprintf("jQuery(this).css({'background-image':'url(%s)'});"
                , $r['image']
        );
    }
    public function getInfo($element) {
        if(array_key_exists($element, $this->elements)) return $this->elements[$element];
        return null;
    }

    public function getRoute($element) {
        $r = $this->getInfo($element);
        if(empty ($r)) return null;
        return $r['route'];
    }

    public function getCategory($element) {
        $r = $this->getInfo($element);
        if(empty ($r)) return null;
        return $r['category'];
    }
    public function getImage($element) {
        $r = $this->getInfo($element);
        if(empty ($r)) return null;
        return $r['image'];
    }
}
